/*
 * Copyright 2014 Alex Curran
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.amlcurran.showcaseview;

import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.util.Optional;

/**
 * 展示区域含有图片渲染效果的
 */
class StandardShowcaseDrawer implements ShowcaseDrawer {
    protected final Paint eraserPaint;
    private int backgroundColour;
    private PixelMap showcaseDrawable;
    private final Paint basicPaint;
    private float showcaseRadius;
    private int mColor = 0;
    private int width;
    private int height;
    private PixelMapHolder pixelMapHolder;
    private boolean isDrawColor = false;

    StandardShowcaseDrawer(Context context) {
        eraserPaint = new Paint();
        eraserPaint.setColor(new Color(0xFFFFFF));
        eraserPaint.setAlpha(0);
        eraserPaint.setBlendMode(BlendMode.CLEAR);
        eraserPaint.setAntiAlias(true);
        eraserPaint.setStyle(Paint.Style.FILL_STYLE);
        basicPaint = new Paint();

        try {
            showcaseRadius = context.getResourceManager().getElement(ResourceTable.Float_showcase_radius).getFloat();
            showcaseDrawable = decodeResource(context, ResourceTable.Media_cling_bleached);
            width = showcaseDrawable.getImageInfo().size.width;
            height = showcaseDrawable.getImageInfo().size.height;
            pixelMapHolder = new PixelMapHolder(showcaseDrawable);
        } catch (Exception e) {
            LocalHiLog.warn("ShowcaseView", "showcase_radius not found");
        }
    }

    private PixelMap decodeResource(Context context, int id) {
        try {
            String path = context.getResourceManager().getMediaPath(id);
            if (path.isEmpty()) {
                return null;
            }
            RawFileEntry assetManager = context.getResourceManager().getRawFileEntry(path);
            ImageSource.SourceOptions options = new ImageSource.SourceOptions();
            options.formatHint = "image/png";
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.editable = true;
            Resource asset = assetManager.openRawFile();
            ImageSource source = ImageSource.create(asset, options);
            return Optional.ofNullable(source.createPixelmap(decodingOptions)).get();
        } catch (Exception e) {
            LocalHiLog.warn("ShowcaseView", "image not found");
        }
        return null;
    }


    @Override
    public void setShowcaseColour(int color) {
        mColor = color;
        isDrawColor = false;
    }


    @Override
    public void drawShowcaseBorder(Canvas canvas, float positionX, float positionY) {
        int left = (int) (positionX - width / 2);
        int top = (int) (positionY - height / 2);
        if (mColor != 0 && !isDrawColor) {
            Canvas showcaseCanvas = new Canvas(new Texture(showcaseDrawable));
            showcaseCanvas.drawColor(mColor, Canvas.PorterDuffMode.SRC_IN);
            showcaseCanvas.save();
            isDrawColor = true;
        }
        canvas.drawPixelMapHolder(pixelMapHolder, left, top, basicPaint);
    }


    @Override
    public void drawShowcase(PixelMap buffer, float positionX, float positionY, float scaleMultiplier) {
        Texture texture = new Texture(buffer);
        Canvas bufferCanvas = new Canvas(texture);
        bufferCanvas.drawCircle(positionX, positionY, showcaseRadius, eraserPaint);

    }

    @Override
    public int getShowcaseWidth() {
        return width;
    }

    @Override
    public int getShowcaseHeight() {
        return height;
    }

    @Override
    public float getBlockedRadius() {
        return showcaseRadius;
    }

    @Override
    public void setBackgroundColour(int backgroundColor) {
        this.backgroundColour = backgroundColor;
    }

    @Override
    public void erase(PixelMap bitmapBuffer) {
        bitmapBuffer.writePixels(this.backgroundColour);
    }


    @Override
    public void drawToCanvas(Canvas canvas, PixelMap bitmapBuffer) {
        PixelMapHolder holder = new PixelMapHolder(bitmapBuffer);
        canvas.drawPixelMapHolder(holder, 0, 0, basicPaint);
    }
}
